package xmltomce.gui;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSeparator;
import javax.swing.LayoutStyle;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import xmltomce.DvdFolderXmlWriter;
import xmltomce.MovieSyncObserver;
import xmltomce.InfoCacheXmlWriter;
import xmltomce.Movie;
import xmltomce.XmlDataMatcher;
import xmltomce.MovieDbSynchronizer;
import xmltomce.properties.MovieSyncProperties;
import xmltomce.properties.PersistentProperties;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("unused")
public class MovieCollectorToMceGui extends javax.swing.JFrame implements MovieSyncObserver {

	private static final long serialVersionUID = -4737048000330781232L;

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}


	private JMenuItem helpMenuItem;
	private JMenu jMenu5;
	private JButton btnStartMatching;
	
	private JButton btnSelectMceDestination;
	private JButton btnSelectDvdFolder;
	private JButton btnSelectXmlSource;
	private JPanel pnlControl;
	private JPanel pnlMceTarget;
	private JPanel pnlDvdSource;
	private JPanel pnlXmlSource;
	private JMenuItem deleteMenuItem;
	private JSeparator jSeparator1;
	private JMenuItem pasteMenuItem;
	private JMenuItem copyMenuItem;
	private JMenuItem cutMenuItem;
	private JMenu jMenu4;
	private JMenuItem exitMenuItem;
	private JSeparator jSeparator2;
	private JMenuItem saveMenuItem;
	private JLabel lblXmlSourcePath;
	private JButton btnDvdFolderSelect;
	private JLabel lblDvdFolderSource;
	private JButton btnXmlSelect;
	private FileDialog fileDialogXmlSource;
	private JProgressBar progressBar;
	private JButton btnInfoCacheSelect;
	private JLabel lblInfoCachePath;
	private JLabel lblXmlDataSourcePath;
	private JLabel lblMediaCenterLogo;
	private JLabel lblDvdFolderPath;
	private JLabel lblDvdLogo;
	private JLabel lblCollectorLogo;
	private JMenu jMenu3;
	private JMenuBar jMenuBar1;

	private PersistentProperties properties = PersistentProperties.getInstance(new MovieSyncProperties());
	private FileDialog fdXmlDataSource;
	private FileDialog fdDvdFolderSource;
	private FileDialog fdMceInfoCacheTarget;
	
	private int movieSyncDone;
	
	
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MovieCollectorToMceGui inst = new MovieCollectorToMceGui();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public MovieCollectorToMceGui() {
		super();
		initFiles();
		initGUI();
	}
	
	private void initFiles(){
		fdXmlDataSource = new FileDialog(properties.getProperty("movieCollectorXmlPath"));
		fdXmlDataSource.getFileChooser().setFileFilter(new FileNameExtensionFilter("MovieCollector XML-Datei", "xml")); // File-Extension Vorgabe (XML)
		
		fdDvdFolderSource = new FileDialog(properties.getProperty("dvdVobFolderPath"));
		JFileChooser fcDvdFolder = fdDvdFolderSource.getFileChooser();
		fcDvdFolder.setAcceptAllFileFilterUsed(false);
		fcDvdFolder.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		
		fdMceInfoCacheTarget = new FileDialog(properties.getProperty("mceInfoCacheFolderPath"));
		JFileChooser fcInfoCache = fdMceInfoCacheTarget.getFileChooser();
		fcInfoCache.setAcceptAllFileFilterUsed(false);
		fcInfoCache.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			setTitle("Movie Database Synchronizer");
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			this.setResizable(false);
			{
				pnlXmlSource = new JPanel();
				GroupLayout pnlXmlSourceLayout = new GroupLayout((JComponent)pnlXmlSource);
				pnlXmlSource.setLayout(pnlXmlSourceLayout);
				pnlXmlSource.setBorder(BorderFactory.createTitledBorder("Xml Data Source"));
				{
					btnSelectXmlSource = fdXmlDataSource.getFileSelectButton();
				}
				{
					lblXmlSourcePath = fdXmlDataSource.getFileLabel();
				}
				{
					lblCollectorLogo = new JLabel();
					lblCollectorLogo.setIcon(getIcon("movieCollectorLogo.png"));
				}
				{
					lblXmlDataSourcePath = new JLabel();
					lblXmlDataSourcePath.setText("temporary Jigloo object...");
					lblXmlDataSourcePath = fdXmlDataSource.getFileLabel();
				}
				{
					btnXmlSelect = new JButton();
					btnXmlSelect.setText("TEMP");
					btnXmlSelect = fdXmlDataSource.getFileSelectButton();
				}
						pnlXmlSourceLayout.setHorizontalGroup(pnlXmlSourceLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblCollectorLogo, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(23)
					.addGroup(pnlXmlSourceLayout.createParallelGroup()
					    .addGroup(GroupLayout.Alignment.LEADING, pnlXmlSourceLayout.createSequentialGroup()
					        .addComponent(btnXmlSelect, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 384, Short.MAX_VALUE))
					    .addGroup(pnlXmlSourceLayout.createSequentialGroup()
					        .addComponent(lblXmlDataSourcePath, GroupLayout.PREFERRED_SIZE, 443, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE)))
					.addContainerGap(19, 19));
						pnlXmlSourceLayout.setVerticalGroup(pnlXmlSourceLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(pnlXmlSourceLayout.createParallelGroup()
					    .addGroup(GroupLayout.Alignment.LEADING, pnlXmlSourceLayout.createSequentialGroup()
					        .addComponent(lblXmlDataSourcePath, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
					        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					        .addComponent(btnXmlSelect, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					    .addGroup(GroupLayout.Alignment.LEADING, pnlXmlSourceLayout.createSequentialGroup()
					        .addComponent(lblCollectorLogo, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, Short.MAX_VALUE))));
			}
			{
				pnlDvdSource = new JPanel();
				GroupLayout pnlDvdSourceLayout = new GroupLayout((JComponent)pnlDvdSource);
				pnlDvdSource.setLayout(pnlDvdSourceLayout);
				pnlDvdSource.setBorder(BorderFactory.createTitledBorder("Dvd Folder Source"));
				{
					btnSelectDvdFolder = fdDvdFolderSource.getFileSelectButton();
				}
				{
					lblDvdFolderPath = fdDvdFolderSource.getFileLabel();
				}
				{
					lblDvdLogo = new JLabel();
					lblDvdLogo.setIcon(getIcon("dvdSourceLogo.png"));
				}
				{
					lblDvdFolderSource = new JLabel();
					lblDvdFolderSource.setText("TEMP");
					lblDvdFolderSource = fdDvdFolderSource.getFileLabel();
				}
				{
					btnDvdFolderSelect = new JButton();
					btnDvdFolderSelect.setText("TEMP");
					btnDvdFolderSelect = fdDvdFolderSource.getFileSelectButton();
				}
						pnlDvdSourceLayout.setHorizontalGroup(pnlDvdSourceLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblDvdLogo, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(26)
					.addGroup(pnlDvdSourceLayout.createParallelGroup()
					    .addComponent(lblDvdFolderSource, GroupLayout.Alignment.LEADING, 0, 449, Short.MAX_VALUE)
					    .addGroup(GroupLayout.Alignment.LEADING, pnlDvdSourceLayout.createSequentialGroup()
					        .addComponent(btnDvdFolderSelect, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 390, Short.MAX_VALUE)))
					.addContainerGap());
						pnlDvdSourceLayout.setVerticalGroup(pnlDvdSourceLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(pnlDvdSourceLayout.createParallelGroup()
					    .addComponent(lblDvdLogo, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addGroup(GroupLayout.Alignment.LEADING, pnlDvdSourceLayout.createSequentialGroup()
					        .addComponent(lblDvdFolderSource, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
					        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					        .addComponent(btnDvdFolderSelect, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))));
			}
			{
				pnlMceTarget = new JPanel();
				GroupLayout pnlMceTargetLayout = new GroupLayout((JComponent)pnlMceTarget);
				pnlMceTarget.setLayout(pnlMceTargetLayout);
				pnlMceTarget.setBorder(BorderFactory.createTitledBorder(null, "Media Center infoCache", TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION));
				{
					lblMediaCenterLogo = new JLabel();
					lblMediaCenterLogo.setIcon(getIcon("mediaCenterLogo.png"));
				}
				{
					lblInfoCachePath = new JLabel();
					lblInfoCachePath.setText("TEMP");
					lblInfoCachePath = fdMceInfoCacheTarget.getFileLabel();
					
				}
				{
					btnInfoCacheSelect = new JButton();
					btnInfoCacheSelect.setText("TEMP");
					btnInfoCacheSelect = fdMceInfoCacheTarget.getFileSelectButton();
				}
				{
					lblInfoCachePath = fdMceInfoCacheTarget.getFileLabel();
				}
				{
					btnSelectMceDestination = fdMceInfoCacheTarget.getFileSelectButton();
				}
						pnlMceTargetLayout.setHorizontalGroup(pnlMceTargetLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblMediaCenterLogo, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(24)
					.addGroup(pnlMceTargetLayout.createParallelGroup()
					    .addGroup(GroupLayout.Alignment.LEADING, pnlMceTargetLayout.createSequentialGroup()
					        .addComponent(btnInfoCacheSelect, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 384, Short.MAX_VALUE))
					    .addGroup(pnlMceTargetLayout.createSequentialGroup()
					        .addComponent(lblInfoCachePath, GroupLayout.PREFERRED_SIZE, 443, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE)))
					.addContainerGap(18, 18));
						pnlMceTargetLayout.setVerticalGroup(pnlMceTargetLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(pnlMceTargetLayout.createParallelGroup()
					    .addGroup(GroupLayout.Alignment.LEADING, pnlMceTargetLayout.createSequentialGroup()
					        .addComponent(lblInfoCachePath, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
					        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, GroupLayout.PREFERRED_SIZE)
					        .addComponent(btnInfoCacheSelect, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					    .addComponent(lblMediaCenterLogo, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)));
			}
			{
				pnlControl = new JPanel();
				pnlControl.setBorder(BorderFactory.createTitledBorder("Commando Panel"));
				{
					btnStartMatching = new JButton();
					pnlControl.add(btnStartMatching);
					btnStartMatching.setText("Start XML -> MCE");
					btnStartMatching.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							convertXmlToMceData();
						}
					});
				}
				{
					pnlControl.add(getProgressBar());
				}
			}
						thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
							.addContainerGap(24, 24)
							.addComponent(pnlXmlSource, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
							.addComponent(pnlDvdSource, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
							.addComponent(pnlMceTarget, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
							.addComponent(pnlControl, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE));
						thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
							.addContainerGap(20, 20)
							.addGroup(thisLayout.createParallelGroup()
							    .addComponent(pnlXmlSource, GroupLayout.Alignment.LEADING, 0, 555, Short.MAX_VALUE)
							    .addComponent(pnlDvdSource, GroupLayout.Alignment.LEADING, 0, 555, Short.MAX_VALUE)
							    .addComponent(pnlMceTarget, GroupLayout.Alignment.LEADING, 0, 555, Short.MAX_VALUE)
							    .addComponent(pnlControl, GroupLayout.Alignment.LEADING, 0, 555, Short.MAX_VALUE))
							.addContainerGap(25, 25));
			{
				jMenuBar1 = new JMenuBar();
				setJMenuBar(jMenuBar1);
				{
					jMenu3 = new JMenu();
					jMenuBar1.add(jMenu3);
					jMenu3.setText("File");
					{
						saveMenuItem = new JMenuItem();
						jMenu3.add(saveMenuItem);
						saveMenuItem.setText("Save selected paths as default");
						saveMenuItem.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								savePathProperties();
							}
						});
					}
					{
						jSeparator2 = new JSeparator();
						jMenu3.add(jSeparator2);
					}
					{
						exitMenuItem = new JMenuItem();
						jMenu3.add(exitMenuItem);
						exitMenuItem.setText("Exit");
						exitMenuItem.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								System.exit(0);
							}
						});
					}
				}
				{
					jMenu4 = new JMenu();
					jMenuBar1.add(jMenu4);
					jMenu4.setText("Edit");
					{
						cutMenuItem = new JMenuItem();
						jMenu4.add(cutMenuItem);
						cutMenuItem.setText("Cut");
					}
					{
						copyMenuItem = new JMenuItem();
						jMenu4.add(copyMenuItem);
						copyMenuItem.setText("Copy");
					}
					{
						pasteMenuItem = new JMenuItem();
						jMenu4.add(pasteMenuItem);
						pasteMenuItem.setText("Paste");
					}
					{
						jSeparator1 = new JSeparator();
						jMenu4.add(jSeparator1);
					}
					{
						deleteMenuItem = new JMenuItem();
						jMenu4.add(deleteMenuItem);
						deleteMenuItem.setText("Delete");
					}
				}
				{
					jMenu5 = new JMenu();
					jMenuBar1.add(jMenu5);
					jMenu5.setText("Help");
					{
						helpMenuItem = new JMenuItem();
						jMenu5.add(helpMenuItem);
						helpMenuItem.setText("Help");
					}
				}
			}
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private ImageIcon getIcon(String iconFileName){
		return new ImageIcon(getClass().getClassLoader().getResource("xmltomce/gui/icons/"+iconFileName));
	}
	
	public JProgressBar getProgressBar() {
		if (progressBar == null){
			progressBar = new JProgressBar();
			progressBar.setPreferredSize(new java.awt.Dimension(507, 14));
		}
		return progressBar;
	}
	
	private void convertXmlToMceData() {
		MovieDbSynchronizer synchronizer = new MovieDbSynchronizer(
				fdXmlDataSource.getSelectedFile(), 
				fdDvdFolderSource.getSelectedFile(),
				fdMceInfoCacheTarget.getSelectedFile());
		Thread synchronizerThread = new Thread(synchronizer);
		
		synchronizer.addObserver(this);
		movieSyncDone = 0;
		btnStartMatching.setEnabled(false);
		synchronizerThread.start();
	}
	
	@Override
	public void movieFolderListSizeChanged(int movieListSize) {
		getProgressBar().setMaximum(movieListSize);
	}

	@Override
	public void movieSyncDone(String movieName) {
		movieSyncDone++;
		getProgressBar().setValue(movieSyncDone);
		
		if (movieSyncDone == getProgressBar().getMaximum()){
			btnStartMatching.setEnabled(true);
		}
	}
	
	private void savePathProperties(){
		properties.setProperty(
				"movieCollectorXmlPath", 
				fdXmlDataSource.getSelectedFile().getAbsolutePath());
		properties.setProperty(
				"dvdVobFolderPath", 
				fdDvdFolderSource.getSelectedFile().getAbsolutePath());
		properties.setProperty(
				"mceInfoCacheFolderPath", 
				fdMceInfoCacheTarget.getSelectedFile().getAbsolutePath());
		properties.save();
	}
	
}
